package com.acme.domain.bank;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.acme.domain.account.Account;

@SuppressWarnings("serial")
public class Client implements Serializable {
	private String name;
	private Gender gender;
	private String emailAddress;
	private List<Account> accounts = new ArrayList<Account>();
	
	public Client(List<Account> accounts, String name, Gender gender, String emailAddress) {
		this(name, gender, emailAddress);
		this.accounts.addAll(accounts);
	}
	
	public Client(List<Account> accounts, String name, Gender gender) {
		this(accounts, name, gender, "");
		this.emailAddress = this.getName() + "@" + "gmail.com";
	}
	
	public Client(String name, Gender gender, String emailAddress) {
		this(name, gender);
		this.emailAddress = emailAddress;
	}
	
	public Client(String name, Gender gender) {
		this.name = name;
		this.gender = gender;
		this.emailAddress = this.getName() + "@" + "gmail.com";
	}
	
	public String getClientSalutation() {
		return this.gender.getGender() + this.name;
	}
	
	public void checkAccount(int id) {
		System.out.println(this.getAccount(id).viewHistory());
	}
	
	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}
	
	public String getName() {
		return name;
	}
	
	public Account getAccount(final int id) {
		return this.accounts.get(id);
	}

	public void addAccount(final Account account) {
		accounts.add(account);
	}

	public List<Account> getAccountsList() {
		return accounts;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(this == obj) return true;
		if(obj == null) return false;
		final Client other = (Client)obj;
		if(!this.name.equals(other.name)) return false;
		return true;
	}

	@Override
	public int hashCode() {
		int hash = 37;
		hash = hash + hash * this.name.hashCode();
		return hash;
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("Client: " + gender.getGender() + name + "\n");
		for(Account account : accounts) {
			sb.append(account.getAccountType().getType() + "account:" + "value: " + account.maximumAmountToWithdraw() + "\n");			
		}
		sb.append("Accounts:" + accounts.size() + "\n\n");
		return sb.toString();
	}

}
